package com.ym.controller.management;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ym.base.enums.FlagEnum;
import com.ym.common.annotations.CustomConstraint;
import com.ym.core.annotation.Log;
import com.ym.core.domain.APIResponse;
import com.ym.core.domain.BaseController;

import com.ym.core.shiro.ShiroUtil;
import com.ym.core.utils.tkmybatis.TK;
import com.ym.medical.domain.entity.AppBannerEntity;
import com.ym.medical.domain.entity.AppStartPageEntity;
import com.ym.medical.domain.entity.CircumRecommendEntity;
import com.ym.medical.domain.entity.NavDeptAdvEntity;
import com.ym.medical.domain.param.*;
import com.ym.medical.domain.vo.NavDeptAdvVo;
import com.ym.medical.enums.ClinicManualStateEnum;
import com.ym.medical.enums.StudioAppStatusEnum;
import com.ym.medical.service.IAppBannerSV;
import com.ym.medical.service.IAppStartPageSV;
import com.ym.medical.service.ICircumRecommendSV;
import com.ym.medical.service.INavDeptAdvSV;

import com.ym.soso.domain.entity.AppHealthPlateEntity;
import com.ym.soso.service.IAppHealthPlateSV;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.List;

/**
 * 内容管理
 *
 * @author: 李涛
 * @version: 2019年06月24日 14:45
 */
@Api(tags = "内容管理")
@RestController
@RequestMapping("/management/content")
@Validated
public class ContentController extends BaseController {

	@Autowired
	private IAppBannerSV appBannerSV;

	@Autowired
	private IAppStartPageSV appStartPageSV;

	@Autowired
	private IAppHealthPlateSV appHealthPlateSV;

	@Autowired
	private INavDeptAdvSV navDeptAdvSV;

	@Autowired
	private ICircumRecommendSV circumRecommendSV;

	@Log(dataBase = false, printBody = false)
	@ApiOperation("banner管理列表")
	@GetMapping(value = "/banner/list")
	public APIResponse<PageInfo<AppBannerEntity>> bannerList(
			AppBannerParam query,
			@RequestParam(defaultValue = "1") int pageNo,
			@RequestParam(defaultValue = "10") int limit
	) {

		PageHelper.startPage(pageNo,limit,"sort_num desc");
		List<AppBannerEntity> clinicManuals = appBannerSV.selectList(
				TK.select(AppBannerEntity.class).column("id", "title", "apply_item", "banner_img",
						"show_location", "source", "part_three_name", "simple", "audit_status",
						"publish_time", "read_num", "forward_num", "publish_status", "create_time",
						" create_person" ,"sort_num")
						.where()
						.andEqualTo(AppBannerEntity::getDeleted,FlagEnum.NO.getKey())
						.end());
		return successPage(clinicManuals);
	}

	@Log(dataBase = false, printBody = false)
	@ApiOperation("举牌管理列表")
	@GetMapping(value = "/dept/list")
	public APIResponse<PageInfo<NavDeptAdvVo>> deptList(
			NavDeptAdvParam query,
			@RequestParam(defaultValue = "1") int pageNo,
			@RequestParam(defaultValue = "10") int limit
	) {

		query.setDeleted(FlagEnum.NO.getKey());
		PageInfo<NavDeptAdvVo> clinicManualPageInfo = navDeptAdvSV.listPageCustomer(query, pageNo, limit);
		return successData(clinicManualPageInfo);
	}

	@Log(dataBase = false, printBody = false)
	@ApiOperation("启动页管理列表")
	@GetMapping(value = "/startPage/list")
	public APIResponse<PageInfo<AppStartPageEntity>> startPageList(
			AppStartPageParam query,
			@RequestParam(defaultValue = "1") int pageNo,
			@RequestParam(defaultValue = "10") int limit
	) {

		PageHelper.startPage(pageNo,limit,"sort_num  desc");
		List<AppStartPageEntity> clinicManualPageInfo = appStartPageSV.selectList(
				TK.select(AppStartPageEntity.class).column("id","title", "apply_item", "banner_img",
						"show_location", "simple", "target_url", "audit_status","publish_time", "read_num",
						"open_time", "status", "sort_num", "create_time", "create_person")
				.where().andEqualTo(AppStartPageEntity::getDeleted,FlagEnum.NO.getKey()).end());
		return successPage(clinicManualPageInfo);
	}

	@Log(dataBase = false, printBody = false)
	@ApiOperation("健康板块管理列表")
	@GetMapping(value = "/health/list")
	public APIResponse<PageInfo<AppHealthPlateEntity>> healthList(
			AppHealthPlateParam query,
			@RequestParam(defaultValue = "1") int pageNo,
			@RequestParam(defaultValue = "10") int limit
	) {

		PageHelper.startPage(pageNo,limit,"sort_num desc ");
		List<AppHealthPlateEntity> clinicManualPageInfo = appHealthPlateSV
				.selectList(
						TK.select(AppHealthPlateEntity.class)
						.column("id", "title", "apply_item", "source", "banner_img", "author", "tag", "model",
								"classify", "index_push","simple", "audit_status", "publish_time", "read_num",
								"forward_num", "publish_status", "sort_num","create_time", "create_person", "deleted")
						.where().andEqualTo(AppHealthPlateEntity::getDeleted,FlagEnum.NO.getKey()).end());

		return successPage(clinicManualPageInfo);
	}

	@Log(dataBase = false, printBody = false)
	@ApiOperation("周边推荐管理列表")
	@GetMapping(value = "/circum/list")
	public APIResponse<PageInfo<CircumRecommendEntity>> circumList(
			CircumRecommendParam query,
			@RequestParam(defaultValue = "1") int pageNo,
			@RequestParam(defaultValue = "10") int limit
	) {
		PageHelper.startPage(pageNo,limit,"sort_num desc");
		List<CircumRecommendEntity> clinicManualPageInfo = circumRecommendSV.selectList(
				TK.select(CircumRecommendEntity.class)
						.where()
						.andEqualTo(CircumRecommendEntity::getDeleted,FlagEnum.NO.getKey()).end()
		);
		return successPage(clinicManualPageInfo);
	}

	@Log(dataBase = false, printBody = false)
	@ApiOperation("banner详情查看")
	@GetMapping(value = "/banner/view")
	public APIResponse<AppBannerEntity> bannerView(
			@RequestParam Long id
	) {
		AppBannerEntity clinicManualPageInfo = appBannerSV.selectById(id);
		return successData(clinicManualPageInfo);
	}

	@Log(dataBase = false, printBody = false)
	@ApiOperation("举牌详情查看")
	@GetMapping(value = "/dept/view")
	public APIResponse<NavDeptAdvEntity> deptView(
			@RequestParam Long id
	) {
		NavDeptAdvEntity clinicManualPageInfo = navDeptAdvSV.selectById(id);
		return successData(clinicManualPageInfo);
	}

	@Log(dataBase = false, printBody = false)
	@ApiOperation("启动页详情")
	@GetMapping(value = "/startPage/view")
	public APIResponse<AppStartPageEntity> startPageView(
			@RequestParam Long id
	) {
		AppStartPageEntity clinicManualPageInfo = appStartPageSV.selectById(id);
		return successData(clinicManualPageInfo);
	}

	@Log(dataBase = false, printBody = false)
	@ApiOperation("健康板块详情")
	@GetMapping(value = "/health/view")
	public APIResponse<AppHealthPlateEntity> healthView(
			@RequestParam Long id
	) {
		AppHealthPlateEntity clinicManualPageInfo = appHealthPlateSV.selectById(id);
		return successData(clinicManualPageInfo);
	}

	@Log(dataBase = false, printBody = false)
	@ApiOperation("周期详情详情")
	@GetMapping(value = "/circum/view")
	public APIResponse<CircumRecommendEntity> circumView(
			@RequestParam Long id
	) {
		CircumRecommendEntity circumRecommend = circumRecommendSV.selectById(id);
		return successData(circumRecommend);
	}


	@ApiOperation("banner新增修改")
	@PostMapping(value = "/banner/update", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public APIResponse bannerUpdate(
			@RequestBody @Validated AppBannerParam appBanner
	) {
		if (appBanner.getId() != null) {
			appBanner.setAuditStatus(StudioAppStatusEnum.TO_AUDIT.getKey());
			appBannerSV.updateById(appBanner.covert());
		} else {
			appBanner.setCreatePerson(ShiroUtil.getLoginId());
			appBanner.setAuditStatus(StudioAppStatusEnum.TO_AUDIT.getKey());
			appBanner.setPublishStatus(ClinicManualStateEnum.NOT_RELEASE.getKey());
			appBannerSV.insert(appBanner.covert());
			//序号设置为ID
			AppBannerEntity updateSort = new AppBannerEntity();
			updateSort.setId(appBanner.getId());
			updateSort.setSortNum(appBanner.getId().intValue());
			appBannerSV.updateById(updateSort);
		}
		return success();
	}

	@ApiOperation("举牌新增修改")
	@PostMapping(value = "/dept/update", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public APIResponse deptUpdate(
			@RequestBody @Validated NavDeptAdvParam appBanner
	) {
		if (appBanner.getId() != null) {
			appBanner.setAuditStatus(StudioAppStatusEnum.TO_AUDIT.getKey());
			navDeptAdvSV.updateById(appBanner.covert());
		} else {
			appBanner.setCreatePerson(ShiroUtil.getLoginId());
			appBanner.setAuditStatus(StudioAppStatusEnum.TO_AUDIT.getKey());
			appBanner.setPublishStatus(ClinicManualStateEnum.NOT_RELEASE.getKey());
			navDeptAdvSV.insert(appBanner.covert());
			//序号设置为ID
			NavDeptAdvEntity updateSort = new NavDeptAdvEntity();
			updateSort.setId(appBanner.getId());
			updateSort.setSortNum(appBanner.getId().intValue());
			navDeptAdvSV.updateById(updateSort);
		}
		return success();
	}

	@ApiOperation("启动页新增修改")
	@PostMapping(value = "/startPage/update", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public APIResponse startPageUpdate(
			@RequestBody @Validated AppStartPageParam appStartPage
	) {
		if (appStartPage.getId() != null) {
			appStartPage.setAuditStatus(StudioAppStatusEnum.TO_AUDIT.getKey());
			appStartPageSV.updateById(appStartPage.covert());
		} else {
			appStartPage.setCreatePerson(ShiroUtil.getLoginId());
			appStartPage.setAuditStatus(StudioAppStatusEnum.TO_AUDIT.getKey());
			appStartPage.setStatus(ClinicManualStateEnum.NOT_RELEASE.getKey());
			appStartPageSV.insert(appStartPage.covert());
			//序号设置为ID
			AppStartPageEntity updateSort = new AppStartPageEntity();
			updateSort.setId(appStartPage.getId());
			updateSort.setSortNum(appStartPage.getId().intValue());
			appStartPageSV.updateById(updateSort);
		}
		return success();
	}

	@ApiOperation("健康档案新增修改")
	@PostMapping(value = "/health/update", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public APIResponse healthUpdate(
			@RequestBody @Validated AppHealthPlateParam appHealthPlate
	) {
		if (appHealthPlate.getId() != null) {
			appHealthPlate.setAuditStatus(StudioAppStatusEnum.TO_AUDIT.getKey());
			appHealthPlateSV.updateById(appHealthPlate.covert());
		} else {
			appHealthPlate.setCreatePerson(ShiroUtil.getLoginId());
			appHealthPlate.setAuditStatus(StudioAppStatusEnum.TO_AUDIT.getKey());
			appHealthPlate.setPublishStatus(ClinicManualStateEnum.NOT_RELEASE.getKey());
			appHealthPlateSV.insert(appHealthPlate.covert());
			//序号设置为ID
			AppHealthPlateEntity updateSort = new AppHealthPlateEntity();
			updateSort.setId(appHealthPlate.getId());
			updateSort.setSortNum(appHealthPlate.getId().intValue());
			appHealthPlateSV.updateById(updateSort);
		}
		return successData(appHealthPlate.getId());
	}

	@ApiOperation("周边新增修改")
	@PostMapping(value = "/circum/update", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public APIResponse circumUpdate(
			@RequestBody @Validated CircumRecommendParam circumRecommend
	) {
		if (circumRecommend.getId() != null) {
			circumRecommendSV.updateById(circumRecommend.covert());
		} else {
			circumRecommend.setCreatePerson(ShiroUtil.getLoginId());
			circumRecommendSV.insert(circumRecommend.covert());
		}
		return successData(circumRecommend.getId());
	}

	@ApiOperation("banner删除")
	@PostMapping(value = "/banner/delete", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public APIResponse operationManualDelete(
			@RequestBody @NotNull Long[] ids
	) {
		appBannerSV.deleteByIds(ids);
		return success();
	}

	@ApiOperation("举牌删除")
	@PostMapping(value = "/dept/delete", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public APIResponse deptDelete(
			@RequestBody @NotNull Long[] ids
	) {
		navDeptAdvSV.deleteByIds(ids);
		return success();
	}

	@ApiOperation("启动页删除")
	@PostMapping(value = "/startPage/delete", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public APIResponse startPageDelete(
			@RequestBody @NotNull Long[] ids
	) {
		appStartPageSV.deleteByIds(ids);
		return success();
	}

	@ApiOperation("健康档案删除")
	@PostMapping(value = "/health/delete", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public APIResponse healthDelete(
			@RequestBody @NotNull Long[] ids
	) {
		appHealthPlateSV.deleteByIds(ids);
		return success();
	}

	@ApiOperation("周边删除")
	@PostMapping(value = "/circum/delete", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public APIResponse circumDelete(
			@RequestBody @NotNull Long[] ids
	) {
		for (Long id : ids) {
			CircumRecommendEntity circumRecommend = new CircumRecommendEntity();
			circumRecommend.setId(id);
			circumRecommend.setDeleted(FlagEnum.IS.getKey());
			circumRecommendSV.updateById(circumRecommend);
		}
		return success();
	}

	@ApiOperation("banner发布")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "ids", value = "主键ID", required = true),
			@ApiImplicitParam(name = "state", value = "发布状态，KEY=ClinicManualStateEnum", required = true)
	})
	@PostMapping(value = "/banner/release", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public APIResponse operationManualRelease(
			@RequestBody @NotNull Long[] ids,
			@RequestParam @NotEmpty @CustomConstraint(ClinicManualStateEnum.class) String state
	) {
		for (Long id : ids) {
			AppBannerEntity appBanner = new AppBannerEntity();
			appBanner.setId(id);
			appBanner.setPublishStatus(state);
			appBanner.setPublishTime(new Date());
			appBannerSV.updateById(appBanner);
		}
		return success();
	}

	@ApiOperation("举牌发布")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "ids", value = "主键ID", required = true),
			@ApiImplicitParam(name = "state", value = "发布状态，KEY=ClinicManualStateEnum", required = true)
	})
	@PostMapping(value = "/dept/release", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public APIResponse deptRelease(
			@RequestBody @NotNull Long[] ids,
			@RequestParam @NotEmpty @CustomConstraint(ClinicManualStateEnum.class) String state
	) {
		for (Long id : ids) {
			NavDeptAdvEntity appBanner = new NavDeptAdvEntity();
			appBanner.setId(id);
			appBanner.setPublishStatus(state);
			appBanner.setPublishTime(new Date());
			navDeptAdvSV.updateById(appBanner);
		}
		return success();
	}

	@ApiOperation("启动页发布")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "ids", value = "主键ID", required = true),
			@ApiImplicitParam(name = "state", value = "发布状态，KEY=ClinicManualStateEnum", required = true)
	})
	@PostMapping(value = "/startPage/release", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public APIResponse startPageRelease(
			@RequestBody @NotNull Long[] ids,
			@RequestParam @NotEmpty @CustomConstraint(ClinicManualStateEnum.class) String state
	) {
		for (Long id : ids) {
			AppStartPageEntity appStartPage = new AppStartPageEntity();
			appStartPage.setId(id);
			appStartPage.setStatus(state);
			appStartPage.setPublishTime(new Date());
			appStartPageSV.updateById(appStartPage);
		}
		return success();
	}

	@ApiOperation("健康档案发布")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "ids", value = "主键ID", required = true),
			@ApiImplicitParam(name = "state", value = "发布状态，KEY=ClinicManualStateEnum", required = true)
	})
	@PostMapping(value = "/health/release", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public APIResponse healthRelease(
			@RequestBody @NotNull Long[] ids,
			@RequestParam @NotEmpty @CustomConstraint(ClinicManualStateEnum.class) String state
	) {
		for (Long id : ids) {
			AppHealthPlateEntity appHealthPlate = new AppHealthPlateEntity();
			appHealthPlate.setId(id);
			appHealthPlate.setPublishStatus(state);
			appHealthPlate.setPublishTime(new Date());
			appHealthPlateSV.updateById(appHealthPlate);
		}
		return success();
	}

	@ApiOperation("banner审核")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "ids", value = "主键ID", required = true),
			@ApiImplicitParam(name = "state", value = "审核状态，KEY=ClinicManualStateEnum", required = true)
	})
	@PostMapping(value = "/banner/audit", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public APIResponse bannerAudit(
			@RequestBody @NotNull Long[] ids,
			@RequestParam @NotEmpty @CustomConstraint(StudioAppStatusEnum.class) String state
	) {
		for (Long id : ids) {
			AppBannerEntity appBanner = new AppBannerEntity();
			appBanner.setId(id);
			appBanner.setAuditStatus(state);
			appBannerSV.updateById(appBanner);
		}
		return success();
	}


	@ApiOperation("举牌审核")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "ids", value = "主键ID", required = true),
			@ApiImplicitParam(name = "state", value = "审核状态，KEY=ClinicManualStateEnum", required = true)
	})
	@PostMapping(value = "/dept/audit", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public APIResponse deptAudit(
			@RequestBody @NotNull Long[] ids,
			@RequestParam @NotEmpty @CustomConstraint(StudioAppStatusEnum.class) String state
	) {
		for (Long id : ids) {
			NavDeptAdvEntity appBanner = new NavDeptAdvEntity();
			appBanner.setId(id);
			appBanner.setAuditStatus(state);
			navDeptAdvSV.updateById(appBanner);
		}
		return success();
	}

	@ApiOperation("启动页审核")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "ids", value = "主键ID", required = true),
			@ApiImplicitParam(name = "state", value = "审核状态，KEY=ClinicManualStateEnum", required = true)
	})
	@PostMapping(value = "/startPage/audit", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public APIResponse startPageAudit(
			@RequestBody @NotNull Long[] ids,
			@RequestParam @NotEmpty @CustomConstraint(StudioAppStatusEnum.class) String state
	) {
		for (Long id : ids) {
			AppStartPageEntity appStartPage = new AppStartPageEntity();
			appStartPage.setId(id);
			appStartPage.setAuditStatus(state);
			appStartPageSV.updateById(appStartPage);
		}
		return success();
	}

	@ApiOperation("健康档案审核")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "ids", value = "主键ID", required = true),
			@ApiImplicitParam(name = "state", value = "审核状态，KEY=StudioAppStatusEnum", required = true)
	})
	@PostMapping(value = "/health/audit", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public APIResponse healthAudit(
			@RequestBody @NotNull Long[] ids,
			@RequestParam @NotEmpty @CustomConstraint(StudioAppStatusEnum.class) String state
	) {
		for (Long id : ids) {
			AppHealthPlateEntity appHealthPlate = new AppHealthPlateEntity();
			appHealthPlate.setId(id);
			appHealthPlate.setAuditStatus(state);
			appHealthPlateSV.updateById(appHealthPlate);
		}
		return success();
	}
}
